package io.renren.api;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import io.renren.entity.WxQrCode;
import io.renren.utils.R;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;


/**
 * @Author: hongyuliu
 * @Date: 2019-05-24
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/qr/code")
public class ApiWxQrcodeController {

    @Resource
    private WxMpService wxMpService;

    public R getQrCode() throws WxErrorException {
        String code = UUID.fastUUID().toString(true);
        //根据机器号，生成临时二维码
        WxMpQrCodeTicket wxMpQrCodeTicket = wxMpService.getQrcodeService().qrCodeCreateTmpTicket(code,300);
        String url = wxMpService.getQrcodeService().qrCodePictureUrl(wxMpQrCodeTicket.getTicket());
        WxQrCode wxQrCode = new WxQrCode();
        wxQrCode.setPurpose(4);
        wxQrCode.setStyle(1);
        wxQrCode.setUrl(url);
        wxQrCode.setCode(code);
        wxQrCode.setName("");
        wxQrCode.setExpire(DateUtil.offset(new Date(), DateField.MINUTE, 5));
        wxQrCode.setDescription("请尽快使用手机微信扫码并完成扫描设置，有效时长五分钟");

        return R.ok();
    }
}
